<?php

class SupplierLocationMenuAction extends CommonAction{
public function index()
{
$map = $this->_search ();
if(trim($_REQUEST['name'])!='')
{
$map['name'] = array('like','%'.trim($_REQUEST['name']).'%');
}
if($this->get('default_map'))
$map = array_merge($map,$this->get('default_map'));
if (method_exists ( $this,'_filter')) {
$this->_filter ( $map );
}
$name=$this->getActionName();
$model = D ($name);
if (!empty ( $model )) {
$this->_list ( $model,$map );
}
$this->display ();
return;
}
public function add()
{
$cate_list = M('SupplierLocationMenuCate')->where('is_delete = 0 and is_effect=1')->findAll();
$this->assign('cate_list',$cate_list);
$this->display();
}
public function insert() {
B('FilterString');
$ajax = intval($_REQUEST['ajax']);
$data = M(MODULE_NAME)->create ();
$this->assign('jumpUrl',u(MODULE_NAME.'/add'));
if(!check_empty($data['name']))
{
$this->error('菜单名称为空!');
}
if(!$data['supplier_location_id'])
{
$this->error('商户名称为空!');
}
$log_info = $data['name'];
$list=M(MODULE_NAME)->add($data);
if (false !== $list) {
foreach($_REQUEST['deal_cate_type_id'] as $type_id)
{
$link_data = array();
$link_data['deal_cate_type_id'] = $type_id;
$link_data['menu_id'] = $list;
M('DealCateTypeMenuLink')->add($link_data);
}
$supplier_l_info['id'] =  $data['supplier_location_id'];
syn_supplier_locationcount($supplier_l_info);
save_log($log_info.L('INSERT_SUCCESS'),1);
$this->success(L('INSERT_SUCCESS'));
}else {
save_log($log_info.L('INSERT_FAILED'),0);
$this->error(L('INSERT_FAILED'));
}
}
public function edit() {
$id = intval($_REQUEST ['id']);
$condition['id'] = $id;
$vo = M(MODULE_NAME)->where($condition)->find();
$supplier_info = M('SupplierLocation')->where('id='.$vo['supplier_location_id'])->find();
$this->assign('supplier_info',$supplier_info);
$this->assign ( 'vo',$vo );
$cate_list = M('SupplierLocationMenuCate')->where('is_delete = 0 and is_effect=1')->findAll();
$this->assign('cate_list',$cate_list);
$this->display ();
}
public function update() {
B('FilterString');
$data = M(MODULE_NAME)->create ();
$log_info = M(MODULE_NAME)->where('id='.intval($data['id']))->getField('name');
$this->assign('jumpUrl',u(MODULE_NAME.'/edit',array('id'=>$data['id'])));
if(!check_empty($data['name']))
{
$this->error('菜单名称为空!');
}
if(!$data['supplier_location_id'])
{
$this->error('商户名称为空!');
}
$list=M(MODULE_NAME)->save ($data);
if (false !== $list) {
M('DealCateTypeMenuLink')->where('menu_id='.$data['id'])->delete();
foreach($_REQUEST['deal_cate_type_id'] as $type_id)
{
$link_data = array();
$link_data['deal_cate_type_id'] = $type_id;
$link_data['menu_id'] = $data['id'];
M('DealCateTypeMenuLink')->add($link_data);
}
$supplier_l_info['id'] =  $data['supplier_location_id'];
syn_supplier_locationcount($supplier_l_info);
save_log($log_info.L('UPDATE_SUCCESS'),1);
$this->success(L('UPDATE_SUCCESS'));
}else {
save_log($log_info.L('UPDATE_FAILED'),0);
$this->error(L('UPDATE_FAILED'),0,$log_info.L('UPDATE_FAILED'));
}
}
public function foreverdelete() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['name'];
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->delete();
if ($list!==false) {
foreach($rel_data as $k=>$v){
$supplier_l_info['id'] =  $v['supplier_location_id'];
syn_supplier_locationcount($supplier_l_info);
}
save_log($info.l('DELETE_SUCCESS'),1);
$this->success (l('DELETE_SUCCESS'),$ajax);
}else {
save_log($info.l('DELETE_FAILED'),0);
$this->error (l('DELETE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
function load_sub_cate()
{
$cate_id = intval($_REQUEST['cate_id']);
$menu_id = intval($_REQUEST['menu_id']);
$sub_cate_list = $GLOBALS['db']->getAll('select c.* from '.DB_PREFIX.'deal_cate_type as c left join '.DB_PREFIX.'deal_cate_type_link as l on l.deal_cate_type_id = c.id where l.cate_id = '.$cate_id);
if($menu_id >0){
foreach($sub_cate_list as $k=>$v)
{
$sub_cate_list[$k]['checked'] = $GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'deal_cate_type_menu_link where deal_cate_type_id = '.$v['id'].' and menu_id = '.$menu_id);
}
}
$this->assign('sub_cate_list',$sub_cate_list);
if($sub_cate_list)
$result['status'] = 1;
else
$result['status'] = 0;
$result['html'] = $this->fetch();
$this->ajaxReturn($result['html'],'',$result['status']);
}
function load_supplier_location()
{
if(intval($GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'supplier_location'))<50000)
$sql  ='select * from '.DB_PREFIX."supplier_location where deal_type=0 AND name like '%".trim($_REQUEST['key'])."%' limit 30";
else 
{
$kws_div = div_str(trim($_REQUEST['key']));
foreach($kws_div as $k=>$item)
{
$kw[$k] = str_to_unicode_string($item);
}
$kw_unicode = implode(' ',$kw);
$sql = 'select * from '.DB_PREFIX."supplier_location where deal_type=0 AND  (match(name_match) against('".$kw_unicode."' IN BOOLEAN MODE)) limit 30";
}
$supplier_location_list = $GLOBALS['db']->getAll($sql);
$this->assign('supplier_location_list',$supplier_location_list);
$this->assign('sid',intval($_REQUEST['sid']));
$this->display();
}
}

?>